using Avalonia.BiliBili.Libs.Window;
using Avalonia.BiliBili.Views.ViewModels;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Bilibili.Common.Database;
using Bilibili.Common.Log;
using Bilibili.Model.Common;
using System;

namespace Avalonia.BiliBili.Views
{
    public partial class SettingView : View
    {
        private MainWindow Window;
        private readonly SettingViewModel viewModel;
        public SettingView()
        {
            InitializeComponent();
            viewModel = new SettingViewModel(this);
        }
        public override void AfterInitializeComponent()
        {
            DataContext = viewModel;
            var tabControl = (Parent as TabItem)?.Parent as TabControl;
            Window = tabControl?.Parent as MainWindow;
            LoadData();
        }
        private void LoadData()
        {
            try
            {
                if (SettingHelper.UseDASH)
                    dashBtn.IsChecked = true;
                else
                    flvBtn.IsChecked = true;
            }
            catch (Exception ex)
            {
                LogManager.Instance.LogError("LoadData", ex);
            }
        }
        public void Callback(InvokeData<object> data)
        {
            try
            {
                switch (data.Name)
                {
                    case "logout":
                        Window.Logout();
                        break;
                }
            }
            catch { }
        }
        private void OnRadioClick(object? sender, RoutedEventArgs e)
        {
            try
            {
                if (sender is RadioButton button)
                {
                    switch (button.GroupName)
                    {
                        case "video":
                            {
                                var value = button.CommandParameter.ToString() == "dash";
                                SettingHelper.UseDASH = value;
                                break;
                            }
                    }
                }
            }
            catch { }
        }
    }
}